package cn.ms.doctor.Service;

import cn.ms.doctor.Entity.*;
import cn.ms.doctor.Mapper.*;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class PrescriptionServiceImpl implements PrescriptionService {

    @Resource
    PrescriptionMapper prescriptionMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    PatientMapper patientMapper;

    @Resource
    OrderMapper orderMapper;

    @Resource
    DrugsorderMapper drugsorderMapper;

    @Resource
    DrugsMapper drugsMapper;


    @Override
    public Map<String, Object> getPrescriptions(Integer userId, Integer roleId, Map<String, Object> params) {
        Map<String, Object> data=new HashMap<>();
        Page<Object> objects = PageHelper.startPage((Integer) params.get("pageIndex"), (Integer) params.get("pageSize"));
        List<Map<String,Object>> pageData=prescriptionMapper.getPrescriptions(userId,roleId,params);
        for (Map<String, Object> pageDatum : pageData) {
            pageDatum.put("userName",userMapper.getUserNameByUserId((Integer) pageDatum.get("userId")));
            pageDatum.put("patientName",patientMapper.getPatientNameByPatientId((Integer) pageDatum.get("patientId")));
            pageDatum.put("patientIdnum",patientMapper.getPatientNameByPatientIdnum((Integer) pageDatum.get("patientId")));
            //根据医生ID，患者ID,诊断ID获取订单ID
            Integer orderId=orderMapper.getOrderIdByUserIdAndPatientIdAndPrescriptionId((Integer) pageDatum.get("userId"),(Integer) pageDatum.get("patientId"),(Integer) pageDatum.get("prescriptionId"));
            //根据订单ID查询药品ID
            List<Integer> Drugs=drugsorderMapper.getDrugsIdByOrderId(orderId);
            //根据所有药品ID查询所有药品名称
            List<String> DrugsData=drugsMapper.getDrugsByDrugIds(Drugs);
            pageDatum.put("drugs",DrugsData);
        }
        data.put("pageData",pageData);
        data.put("pageIndex",objects.getPageNum());
        data.put("pageSize",objects.getPageSize());
        data.put("pageCount",objects.getPages());
        return data;
    }


    @Override
    @Transactional
    public boolean addPrescription(User user, Map<String, Object> params) {
        Prescription prescription=new Prescription(null,user.getUserId(),(Integer) params.get("patientId"), (String) params.get("prescriptionDiagnosis"));
        if (!prescriptionMapper.addPrescription(prescription)) {
            throw new RuntimeException("添加诊断异常");
        }
        List<Drugsorder> drugs = (List<Drugsorder>) params.get("drugs");
        System.out.println(JSON.toJSONString(drugs));
        BigDecimal drugsPrice=drugsMapper.getPriceSumByPrice(drugs);
        Order order=new Order(null,prescription.getUserId(),prescription.getPatientId(),prescription.getPrescriptionId(),new Date(),null,null,drugsPrice,0);
        if (!orderMapper.addOrder(order)){
            throw new RuntimeException("添加订单异常");
        }

        if (!drugsorderMapper.addDrugsAndOrder(order.getOrderId(), drugs)){
            throw new RuntimeException("添加药品订单异常");
        }
        List<Object> drugsorders=JSON.parseArray(JSON.toJSONString(drugs));
        for (Object drugsorder : drugsorders) {
            if (!drugsMapper.updateDrugsCountByDrugsId(drugsorder)){
                throw new RuntimeException("修改库存异常");
            }
        }
        return true;
    }
}
